package com.example.demo.model.meta;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.config.QueryExpression;
import com.baidu.bas.annotation.model.Tenancy;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.Searchable;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.base.HyperModel;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/11/20
 */
@Setter
@Getter
@Erupt(name = "逻辑表",orderBy = "id desc")
@Table(name = "ma_meta_logical_table",uniqueConstraints = {
        @UniqueConstraint(columnNames = {"name","logical_catalog_id"},name = "unq_table_name_catalog")
})
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@DynamicUpdate
@NoArgsConstructor
@Accessors(chain = true)
public class LogicalTable extends HyperModel {
    /**
     * 名称
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String name;
    /**
     * 中文名称
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String cname;
    /**
     * 分层, eg: ods, dwd
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String layer = "default";
    /**
     * 备注
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.TEXTAREA)
    )
    protected String remarks;
    /**
     * 数据目录
     */
    @ManyToOne(optional = true)
    @JoinColumn(foreignKey = @ForeignKey(name = "fk_logical_catalog_1"))
    @EruptField(
            views = @View(title = "逻辑目录", column = "name"),
            edit = @Edit(title = "逻辑目录", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    protected LogicalCatalog logicalCatalog;
    /**
     * 是否使用one id
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean useOneId = false;
    /**
     * 是否为事实表
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean isFact = true;

    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean enable = true;

    @OneToMany(mappedBy="logicalTable",cascade=CascadeType.ALL,fetch=FetchType.LAZY,orphanRemoval = true)
    protected List<Column> columnList;

    @ElementCollection
    @CollectionTable(name = "ma_meta_table_tags", joinColumns = @JoinColumn(name = "id"))
    @javax.persistence.Column(name = "tags")
    protected Set<String> tags;

    @javax.persistence.Column(name = "tenancy")
    @EruptField(
            views = @View(title = "租户"),
            edit = @Edit(title = "租户"),
            searchable = @Searchable(omitBlank = false,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    @Tenancy
    private String tenancy;

    public LogicalTable setColumnList(List<Column> columnList){
        this.columnList = columnList;
        for(Column column : columnList){
            column.setLogicalTable(this);
        }
        return this;
    }

    public LogicalTable(String name) {
        this.name = name;
        this.cname = name;
    }

    public LogicalTable(String name, String cname) {
        this.name = name;
        this.cname = cname;
    }
}
